package net.losjuegos.web.controladores;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.losjuegos.core.dominios.Bebida;
import net.losjuegos.core.dominios.Bebida.BebidaType;
import net.losjuegos.core.factories.BebidaFactorySingleton;
import net.losjuegos.core.servicios.BebidaService;

/**
 * Servlet implementation class BebidaServlet
 */
@WebServlet("/bebida/*")
public class BebidaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public BebidaServlet() {

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out
				.println("doGet - getRequestURI = " + request.getRequestURI());
		System.out.println("doGet - getServletPath = "
				+ request.getServletPath());
		String action = extraerAction(request);
		System.out.println("doGet - action = " + action);

		if (action.equals("alta")) {
			request.setAttribute("bebidasType", BebidaType.values());
			alta(request, response);
		} else {
			if (action.equals("baja")) {
				request.setAttribute("bebidasType", BebidaType.values());
				baja(request, response);
			} else {
				if (action.equals("editar")) {
					request.setAttribute("bebidasType", BebidaType.values());
					editar(request, response);
				} else {
					listarBebidas(request, response);
				}
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doPost - getRequestURI = "
				+ request.getRequestURI());
		System.out.println("doPost - getServletPath = "
				+ request.getServletPath());
		System.out.println("doPost - getContextPath = "
				+ request.getContextPath());
		String action = extraerAction(request);
		System.out.println("doPost - action = " + action);

		if (action.equals("alta")) {
			actionAlta(request, response);
		} else {
			if (action.equals("baja")) {
				actionBaja(request, response);
			} else {
				if (action.equals("editar")) {
					actionEditar(request, response);
				} else {
					throw new RuntimeException("Algo no anduvo bien!");
				}
			}
		}
	}

	private void actionAlta(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String nombreBebida = request.getParameter("nombre");
		String tipoBebida = request.getParameter("tipo");

		System.out.println("nombreBebida= " + nombreBebida);
		System.out.println("tipoBebida= " + tipoBebida);

		BebidaType bt = BebidaType.valueOf(tipoBebida);
		Bebida b = BebidaFactorySingleton.getInstance().crearBebida(bt);
		b.setNombre(nombreBebida);

		BebidaService.getInstance().agregarBebida(b);

		response.sendRedirect(request.getContextPath() + "/bebida/list");
	}

	private void actionBaja(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String nombreBebida = request.getParameter("nombre");
		String tipoBebida = request.getParameter("tipo");
		int id = Integer.parseInt(request.getParameter("id"));
		System.out.println("nombreBebida= " + nombreBebida);
		System.out.println("tipoBebida= " + tipoBebida);
		System.out.println("id= " + id);

		Bebida b = BebidaService.getInstance().findById(id);
		
		if (b != null) {
			b.setNombre(nombreBebida);
			b.setId(id);
			BebidaService.getInstance().eliminarBebida(b);
		}
		response.sendRedirect(request.getContextPath() + "/bebida/list");
	}

	private void actionEditar(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException {
		String nombreBebida = request.getParameter("nombre");
		String tipoBebida = request.getParameter("tipo");
		int id = Integer.parseInt(request.getParameter("id"));
		System.out.println("nombreBebida= " + nombreBebida);
		System.out.println("tipoBebida= " + tipoBebida);
		System.out.println("id= " + id);
		Bebida b = BebidaService.getInstance().findById(id);
		BebidaType bt = BebidaType.valueOf(tipoBebida);
		Bebida b1 = BebidaFactorySingleton.getInstance().crearBebida(bt);
		b1.setNombre(nombreBebida);
		b1.setId(b.getId());
		BebidaService.getInstance().actualizarBebida(b1);
		response.sendRedirect(request.getContextPath() + "/bebida/list");
	}

	// //////////////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////

	private void alta(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/bebida/alta.jsp").include(
				request, response);
	}

	private void baja(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		actionBaja(request, response);
	}

	private void editar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException { 
	     	int id = Integer.parseInt(request.getParameter("id"));
		 	System.out.println("id=" + id);
		    Bebida b = BebidaService.getInstance().findById(id);
		    request.setAttribute("bebida", b);
		    request.getRequestDispatcher("/WEB-INF/bebida/editar.jsp").include(
				request, response);
	}

	private void listarBebidas(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Bebida> bebidas = BebidaService.getInstance().listaBebidas();
		request.setAttribute("bebidas", bebidas);
		request.getRequestDispatcher("/WEB-INF/bebida/lista.jsp").include(
				request, response);
	}

	// //////////////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////
	// ///////// H E L P E R S
	// //////////////////////////////////////////////////////////////////////////
	// //////////////////////////////////////////////////////////////////////////

	private String extraerAction(HttpServletRequest request) {
		String uri = request.getRequestURI();
		String action = uri.substring(uri.lastIndexOf('/') + 1);

		return action;
	}

}
